﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FW;
using System.Threading;

namespace Kcadastro.classes
{
    public class servidores
    {
        private fwlog log = (fwlog)ConfigurationManager.GetSection("fwlog");

        kcadDataContext db = new kcadDataContext(ConfigurationManager.ConnectionStrings["KcadConnectionString"].ToString());

        private string _msg;
        public string MSG { get { return _msg; } }

        public servidores()
        { 
        
        }

        public List<tb_VM> listaMaquinas()
        {
            List<tb_VM> ret = (from tb in db.tb_VMs
                               select tb).ToList<tb_VM>();

            return ret;
        }
        public List<tb_wsRota> listaRotas()
        {
            List<tb_wsRota> ret = (from tb in db.tb_wsRotas
                                   orderby tb.ordem ascending
                               select tb).ToList<tb_wsRota>();

            return ret;
        }
        public tb_VM consultaVM(int _idmaq)
        {
            tb_VM ret = db.tb_VMs.Single(t => t.PK_IDmaq == _idmaq);
            return ret;
        }

        public void gravarMaquina(tb_VM _tbv)
        {
            tb_VM tb = new tb_VM();
            if (_tbv.PK_IDmaq != 0)
            {
                tb = db.tb_VMs.Single(t => t.PK_IDmaq == _tbv.PK_IDmaq);
            }
            tb.ativa = _tbv.ativa;
            tb.local = _tbv.local;
            tb.nome = _tbv.nome;
            tb.ordem = _tbv.ordem;
            tb.qtdvms = _tbv.qtdvms;
            tb.url = _tbv.url;
            tb.wsfim = _tbv.wsfim;
            tb.wsini = _tbv.wsini;
            if (_tbv.PK_IDmaq == 0)
            {
                db.tb_VMs.InsertOnSubmit(tb);
            }
            db.SubmitChanges();

        }

        public void reconstruirRotas()
        {
            List<tb_VM> ltv = (from tb in db.tb_VMs
                               where tb.ativa == true
                               orderby tb.ordem
                               select tb).ToList<tb_VM>();
            db.ExecuteCommand("delete from tb_wsrotas", new object[0]);
            db.SubmitChanges();
            foreach (tb_VM vm in ltv)
            {
                for (int x = (int)vm.wsini; x <= (int) vm.wsfim; x++)
                {
                    tb_wsRota rota = new tb_wsRota()
                    {
                        ativo = true,
                        dt_ult = DateTime.Now,
                        local = vm.nome,
                        FK_IDmaq = vm.PK_IDmaq,
                        dt_ultteste = new DateTime(1901, 1, 1),
                        rurl = vm.url + string.Format("/dbiwstt{0}/dbiws.dll/soap/IDBIws", x.ToString("00")),
                        ordem = x * 100 + vm.ordem,
                        noar = false
                    };
                    db.tb_wsRotas.InsertOnSubmit(rota);
                    db.SubmitChanges();
                }
            }

            List<tb_wsRota> lrt = (from tb in db.tb_wsRotas
                                   orderby tb.ordem
                                   select tb).ToList<tb_wsRota>();

            foreach (tb_wsRota rt in lrt)
            {
                tb_wsRota urt = db.tb_wsRotas.Single(t => t.PK_ID == rt.PK_ID);
                urt.dt_ult = DateTime.Now;
                db.SubmitChanges();
                Thread.Sleep(2);
            }
        }

        public void apagarVM(int _idmaq)
        {
            tb_VM tv = db.tb_VMs.Single(t => t.PK_IDmaq == _idmaq);
            db.tb_VMs.DeleteOnSubmit(tv);
            db.SubmitChanges();
        }
    }

}